package com.huawei.control.phonesinfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.huawei.bean.Phones;

import com.huawei.model.PhoneInfoModel;
import com.huawei.model.UserInfoModel;
import com.huawei.utils.EasyFactory;
import com.huawei.utils.MyUtils;

@WebServlet("/PhonesInfoAjaxQueryControl")
public class PhonesInfoAjaxQueryControl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Phones phone = null;
		String strFlag = request.getParameter("flag");
		if (strFlag != null && strFlag.equals("1")) {
			phone = (Phones) request.getSession().getAttribute("deleteQueryParamBean");
			List<Phones> lst = EasyFactory.getNewInstance(PhoneInfoModel.class).queryPhoneInfoByParams(phone);
			// 根据业务逻辑结果进行显示页面跳转处理
			// 将查询的结果集缓存到session对象中
			request.getSession().setAttribute("lstQueryUserInfos", lst);
			// 保留查询条件为 删除之后再次使用
			request.getSession().setAttribute("deleteQueryParamBean", phone);

			// 转发到查询成功页面
			request.getRequestDispatcher("WEB-INF/pages/phonesinfo/PhonesInfoAddDetail.jsp").forward(request, response);
		} else {
			// 使用form表单的条件进行查询
			// 将表单转换为参数实体类
			phone = MyUtils.convertRequest2BeanWithFileUpload(request, Phones.class);
			// 调用Medol进行业务逻辑
			List<Phones> lst = EasyFactory.getNewInstance(PhoneInfoModel.class).queryPhoneInfoByParams(phone);

			// 把查询结果集转换为json字符串格式，借助于第三方工具 Alibaba 的 fastjson工具 （进行了日期格式转换处理）

			String strJson = JSON.toJSONStringWithDateFormat(lst, "yyyy-MM-dd HH:mm:ss",
					SerializerFeature.WriteDateUseDateFormat);
			// 统一编码
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			// 获取response中的流操作对象PrintWriter
			PrintWriter pw = response.getWriter();
			// 写出内容到浏览器客户端
			pw.write(strJson);
			pw.flush();

		}

	}
}
